package net.zjitc.utils;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/11/15 9:01
 * 4
 */
public class MailDemo {
    private static void sendMail(String to,String context)throws Exception{
        /**
         * 1.和服务器建立对话   session
         * 名称。  端口。  授权用户
         */
        Properties properties = new Properties();
        //qq的邮箱服务器
        properties.setProperty("mail.host", "smtp.qq.com");
        properties.setProperty("mail.smtp.prot", "587");
        properties.setProperty("mail.smtp.auth", "true");

        //授权给用户
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("493674023@qq.com", "hstjafivjbbgbhbc");
            }
        };
        //会话建立
        Session session = Session.getInstance(properties, authenticator);

        /**
         * 创建一个邮件对象（收件人，抄送，密送给，标题，内容 ）
         */
        Message message = new MimeMessage(session);
        //设置内容
        //1.谁发的
        message.setFrom(new InternetAddress("493674023@qq.com"));
//        message.setRecipient(Message.RecipientType.TO,new InternetAddress("493674023@qq.com"));
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
        message.setSubject("软件1704邮箱测试");
//        message.setContent("aaa","text/html;charset=utf-8");
//        message.setContent("<a href = \"http://www.baidu.com\">"+context+"</a>","text/html;charset=utf-8");
        /**
         * 3.发送
         */
        Transport.send(message);
    }
}
